using System;
using System.Collections.Generic;
using SimpleJSON;
using UnityEngine;

namespace Gj.DarkLoong
{
    public class Event
    {
        public static void Trigger(string e, string id, JSONObject meta, Action<bool, string> callback)
        {
            Debug.Log($"trigger event: {e} {id}");
            if (!Auth.IsLogin()) return;
            Project.ProjectUpdate.Queue.Enqueue(async () =>
            {
                Debug.Log($"trigger event: {e} {id}");
                var data = new JSONObject();
                data["id"] = id;
                data["meta"] = meta;
                var header = Api.CookieHeader();
                header["token"] = Auth.SessionToken;
                var response = await Api.Get($"/workshop/event/trigger/{e}", data, header);

                var err = response["status"].AsInt != 200 ? response["message"].Value : "";
                var result = response["result"];

                callback(result.AsArray, err);
            });

        }

        public static void Unique(string e, string id, JSONObject meta, Action<bool, string> callback)
        {
            Debug.Log($"unique event: {e} {id}");
            if (!Auth.IsLogin()) return;
            Project.ProjectUpdate.Queue.Enqueue(async () =>
            {
                Debug.Log($"unique event: {e} {id}");
                var data = new JSONObject();
                data["id"] = id;
                data["meta"] = meta;
                var header = Api.CookieHeader();
                header["token"] = Auth.SessionToken;
                var response = await Api.Get($"/workshop/event/unique/{e}", data, header);

                var err = response["status"].AsInt != 200 ? response["message"].Value : "";
                var result = response["result"];

                callback(result.AsArray, err);
            });

        }
    }
}